/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelSignIn.java
 *
 * Created on 19/11/2009, 19:43:07
 */

package desktopapplication1;


import BackEnd.User;
import BackEnd.Validation;
import Services.Security;
import java.text.MessageFormat;
import java.util.ResourceBundle;
import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationContext;

/**
 *
 * @author Gabi
 */
public class PanelSignIn extends javax.swing.JPanel {

    /** Creates new form PanelSignIn */
    public PanelSignIn() {
        initComponents();
    }

    @Action
    public void signIn() {
        jLabel3.setText("");
        jLabel4.setText("");
        ResourceBundle bundle= ResourceBundle.getBundle("desktopapplication1/resources/PanelSignIn");
	boolean flag=false;


        if(!Validation.validaString(user.getText())) {
            jLabel3.setText(bundle.getString("validUser"));
            user.setText("");
            flag=true;
        }
        if(!Validation.validaString(pass.getText())) {
            jLabel4.setText(bundle.getString("validPass"));
            pass.setText("");
            flag=true;
        }
	User actualUser;
        ApplicationContext ctx=Application.getInstance().getContext();
        if(!flag) {
	if(!((actualUser=(Security.signIn(user.getText(), pass.getText()))) == null)){
	    Object[] args={actualUser.getUserName()};
            MessageFormat form = new MessageFormat(ctx.getResourceMap().getString("accountSignIn.logged"));
            rta.setText(form.format(args));
            
	    actualUser.setPassword(pass.getText());

             Object[] args2={actualUser.getUserName()};
            MessageFormat form2 = new MessageFormat(ctx.getResourceMap().getString("accountDialog.alreadyLogged"));
            mainWin.status.setText(form2.format(args2));
            mainWin.userPanel.updateUI();
	}
	else{

            rta.setText(ctx.getResourceMap().getString("accountSignIn.wrongPass"));
	}
	mainWin.setUser(actualUser);
        }
    }




    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        signInLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        user = new javax.swing.JTextField();
        pass = new javax.swing.JPasswordField();
        signInButton = new javax.swing.JButton();
        rta2 = new javax.swing.JLabel();
        rta = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplication1.DesktopApplication1.class).getContext().getResourceMap(PanelSignIn.class);
        signInLabel.setFont(resourceMap.getFont("signInLabel.font")); // NOI18N
        signInLabel.setText(resourceMap.getString("signInLabel.text")); // NOI18N
        signInLabel.setName("signInLabel"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        user.setText(resourceMap.getString("user.text")); // NOI18N
        user.setName("user"); // NOI18N
        user.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userActionPerformed(evt);
            }
        });

        pass.setText(resourceMap.getString("pass.text")); // NOI18N
        pass.setName("pass"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(desktopapplication1.DesktopApplication1.class).getContext().getActionMap(PanelSignIn.class, this);
        signInButton.setAction(actionMap.get("signIn")); // NOI18N
        signInButton.setText(resourceMap.getString("signInButton.text")); // NOI18N
        signInButton.setName("signInButton"); // NOI18N

        rta2.setText(resourceMap.getString("rta2.text")); // NOI18N
        rta2.setName("rta2"); // NOI18N

        rta.setText(resourceMap.getString("rta.text")); // NOI18N
        rta.setName("rta"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rta, javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(user)
                                    .addComponent(pass, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                                .addComponent(signInButton))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(signInLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 218, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rta2, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel5))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(125, 125, 125)
                        .addComponent(rta2))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(signInLabel)
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(signInButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addGap(5, 5, 5)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(pass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(4, 4, 4)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rta, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addContainerGap(111, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void userActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userActionPerformed
	// TODO add your handling code here:
    }//GEN-LAST:event_userActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPasswordField pass;
    private javax.swing.JLabel rta;
    private javax.swing.JLabel rta2;
    private javax.swing.JButton signInButton;
    private javax.swing.JLabel signInLabel;
    private javax.swing.JTextField user;
    // End of variables declaration//GEN-END:variables

}
